import 'package:flutter/material.dart';
import 'package:flutter_public/flutter_public.dart';

import '../model/test_model.dart';
import '../notifier/test_list_notifier.dart';

/// FileName test_filter_drawer
///
/// @Author lax
/// @Date 2024/6/4
///
/// @Description

class TestFilterDrawer extends StatefulWidget {
  final TestModel model;

  final Function(TestModel model)? onFilter;

  const TestFilterDrawer({
    super.key,
    required this.model,
    this.onFilter,
  });

  @override
  State<StatefulWidget> createState() {
    return TestFilterDrawerState();
  }

}

class TestFilterDrawerState extends State<TestFilterDrawer> {
  late TestModel model;

  @override
  void initState() {
    super.initState();
    model = widget.model;
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: SafeArea(
        child: GestureDetector(
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus &&
                currentFocus.focusedChild != null) {
              FocusManager.instance.primaryFocus?.unfocus();
            }
          },
          child: Column(
            children: [
              Container(
                alignment: Alignment.center,
                height: 44,
                child: const Text(
                  '筛选',
                  style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.bold,
                      fontSize: 22),
                ),
              ),
              Expanded(
                child: listView(context),
              ),
              SeparatedRow(
                padding: const EdgeInsets.all(16),
                separator: const SizedBox(width: 16),
                expanded: true,
                children: [
                  BorderButton(
                    onPressed: () {
                      setState(() {
                        model = TestModel();
                      });
                    },
                    text: '重置',
                  ),
                  PublicButton(
                    onPressed: () {
                      widget.onFilter?.call(model);
                    },
                    text: '确定',
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget listView(BuildContext context) {
    return SeparatedScrollColumn(
      padding: const EdgeInsets.symmetric(vertical: 16),
      separator: const PaddingDivider(),
      children: [
        TextSelectItem(
          title: 'corp',
          value: model.corpName,
          onTap: () async {
            Loading.show();
            final resp = await Api.get(
                base: TestListNotifier.base,
                url: 'api/base/orgmanager/GetRoleCorp');
            Loading.dismiss();
            final list = resp['Result'] ?? [];
            final dataSource = list
                .map<KeyValueModel>((e) => KeyValueModel(
              value: e['CropName'] ?? '',
              key: e['CorpID'].toString(),
            ))
                .toList();
            if (!context.mounted) {
              return;
            }
            ModalBottomSheet.showSingleSheet(
              context: context,
              title: 'selectCorp',
              dataSource: dataSource,
              selectedId: model.corp,
              onConfirm: (value) {
                setState(() {
                  model.corp = value?.key;
                  model.corpName = value?.value;
                });
              },
            );
          },
        ),
        TextSelectItem(
          title: '状态',
          value: model.statusName,
          onTap: () async {
            ModalBottomSheet.showSingleSheet(
              context: context,
              title: '请选择',
              dataSource: [
                KeyValueModel(key: '0', value: '保存'),
                KeyValueModel(key: '1', value: '进行中'),
                KeyValueModel(key: '2', value: '延迟'),
                KeyValueModel(key: '3', value: '完成'),
              ],
              selectedId: model.status,
              onConfirm: (value) {
                setState(() {
                  model.status = value?.key;
                  model.statusName = value?.value;
                });
              },
            );
          },
        ),
      ],
    );
  }
}
